package lxh.com.union.base.base;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.Unbinder;
import lxh.com.union.base.kit.KnifeKit;

/**
 * Created by Administrator on 2017/7/3.
 */

public abstract class BaseFragment extends Fragment {
    protected Unbinder mUnbinder;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBeforView(savedInstanceState);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(getContentView(),container,false);
        mUnbinder=KnifeKit.bind(this,view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getBundle(getArguments());
    }



    /**
     * view创建之前要做的初始化
     * @param savedInstanceState
     */
    protected void initBeforView(Bundle savedInstanceState) {

    }

    /**
     * 返回当前fragment的布id
     * @return
     */
    protected abstract int getContentView();

    /**
     * 初始化界面
     * @param view
     */
    protected abstract void initView(View view);

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 获取activity传过来的数据
     * @param arguments
     */
    protected void getBundle(Bundle arguments) {

    }

    /**
     * 启动activity
     * @param cls
     */
    protected void startActivity(Class<?> cls){
        startActivity(new Intent(getContext(),cls));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        KnifeKit.unbind(mUnbinder);
    }
}
